package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.mapper.UserMapper;
import com.qf.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * @author mkr
 * @version V1.0
 * @Project java2203-mkr
 * @Package com.qf.service
 * @Description:
 * @Date 2022/6/9 11:29
 */
    @Service
    public class UserServiceImpl implements UserService {

        @Autowired
        private UserMapper userMapper;

        //会报错, 无法注入, 因为request, response, sesson, model是springmvc的默认支持参数对象
        //是子容器中的对象, 而这里service属于spring父容器管理的对象, 无法使用子容器中的对象
        //@Autowired
        //private HttpServletRequest request;

        @Override
        public List<User> findUserAll() {
            //调用dao持久层
            List<User> list = userMapper.selectList(null);
            return list;
        }

        @Override
        public List<User> findByNameAndSexAndBirthday() {
            //1. 创建查询条件构造器
            QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

            //2. 设置根据姓名模糊查询条件
            queryWrapper.like("name", "王");

            //3. 设置根据性别精确查询条件
            queryWrapper.eq("sex", "男");

            //4. 设置根据日期范围查询条件
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            Date date = null;
            try {
                date = sdf.parse("2021-02-01 00:00:00");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            queryWrapper.ge("birthday", date);



            //5. 调用dao查询并返回结果
            List<User> list = userMapper.selectList(queryWrapper);
            //6. 返回
            return list;
        }

    /**
     * 添加用户
     * @return
     */
    @Override
    public User addUser() {

        User user = new User();

        user.setId(12);
        user.setName("王老五");
        user.setPassword("123");
        user.setSex("男");
        LocalDate date = LocalDate.now();
        user.setBirthday(new Date(2022-06-06));
        user.setRegistTime(new Date());

        //2.调用dao层
        userMapper.insert(user);

        return user;
    }

    /**
     * 根据ID更新用户信息
     * @return
     */
    @Override
    public User updateUser() {
        //1. 创建查询条件构造器
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

        //2. 设置根据性别精确查询条件
        queryWrapper.eq("id", "10");
        User user = new User();
        user.setId(10);
        user.setName("王");
        user.setPassword("123");
        user.setSex("男");
        LocalDate date = LocalDate.now();
        user.setBirthday(new Date());
        user.setRegistTime(new Date());

        userMapper.updateById(user);

        return null;
    }

    @Override
    public void deleteUser(int id) {
        //1. 创建查询条件构造器
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();

        //精确查找
        queryWrapper.eq("id", id);

        userMapper.deleteById(id);
    }

}
